﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateStockHandler : ICommandHandler<CreateOrUpdateStockCommand>
    {
        private readonly IStockRepository stockRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateStockHandler(IStockRepository stockRepository, IUnitOfWork unitOfWork)
        {
            this.stockRepository = stockRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateStockCommand command)
        {
            var stock = new Stock
            {
                ID = command.ID,
                Address=command.Address,
                Code=command.Code,
                CurrentCompanyID=command.CurrentCompanyID,
                Description=command.Description,
                MaxQuatity=command.MaxQuatity,
                Name=command.Name,
                Purchases=command.Purchases,
                Type=command.Type,
            };
            if (stock.ID == 0)
                stockRepository.Add(stock);
            else
                stockRepository.Update(stock);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateStockCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateStockCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
